package cn.core.cron.interceptors;

import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.quartz.SchedulerFactoryBeanCustomizer;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

import javax.annotation.Resource;
import javax.sql.DataSource;

/**
 * 定时任务 >>> Quartz 配置
 */
@Configuration
public class QuartzSchedulerFactoryBeanConfiguration implements SchedulerFactoryBeanCustomizer {

  @Resource
  private DataSource dataSource;

  @Value("${spring.quartz.data-source-name}")
  private String dataSourceName;

  @Override
  public void customize(SchedulerFactoryBean schedulerFactoryBean) {
    schedulerFactoryBean.setDataSource(((DynamicRoutingDataSource) dataSource).getDataSource(dataSourceName));
  }

}
